<?php
    session_start();
    if(isset($_SESSION['usuario'])){
        $user = $_SESSION['usuario'];
        $_SESSION['usuario'] = $user;
    } else {
        header('Location: ../');
    }
    include_once '../Model/OrdenDetailDao.php';
    include_once '../Model/OrdenDao.php';
    include_once '../Model/StepLogDao.php';

    if(isset($_GET['act'])){
        $action = $_GET['act'];
        if($action=='insert'){
            $json = $_POST['data'];
            $jsonDecode = json_decode($json, true);
            $data = $jsonDecode['data'];
            $idOrden = $_GET['idOrden'];

            if($idOrden=='0'){ // nueva orden
                $resTemp = getMaxOrden($user);
                $nAntes = $resTemp[0]['idOrden'];
                insertarOrden($user);
                $resTemp = getMaxOrden($user);
                $nDesp = $resTemp[0]['idOrden'];
                if($nAntes==$nDesp){ echo '-2'; }   // no pudo crear la orden
                else {$idOrden = $nDesp; }
            }
            if($idOrden!='0'){
                $ordenDetails = $_GET['ordenDetail'];
                for($i=0;$i<sizeof($data);$i++){
                    $descuento = '0';
                    $precio = '0';
                    if(isset($data[$i]['Discount']) && $data[$i]['Discount']!=''){
                        $descuento = $data[$i]['Discount'];
                    }
                    if(isset($data[$i]['UnitPrice']) && $data[$i]['UnitPrice']!=''){
                        $precio = $data[$i]['UnitPrice'];
                    }
                    $insert[$i] = "INSERT INTO `u225435723_orden`.`ORDEN_DETAIL` "
                        . "(`idOrden`,`cantidad`,`descripcion`,`proveedor`,"
                        . "`precioUnitario`,`discount`,`tipoOrdenCode`) VALUES ("
                        .$idOrden .","
                        .$data[$i]['Quantity'] . ",'"
                        .$data[$i]['Description'] ."','"
                        .$data[$i]['Provider'] ."',"
                        .$precio . ","
                        .$descuento . ","
                        . "'COM')";
                    insertarLog($idOrden,'CRE','PAC',$user,$_GET['comment']);

                }
                if(sizeof($data)==0){            // no hay data a ingresar
                    echo -1;

                } else {
                    echo insertarOrdenDetail($insert, $user, $idOrden);
                }
            }
        } elseif ($action=='delete') {
            $data = $_POST['data'];
            eliminarOrdenDetail($data);
            echo 'delete';
        } elseif ($action=='update') {
            $idOrden = $_GET['idOrden'];
            $json = $_POST['data'];
            $jsonDecode = json_decode($json, true);
            $data = $jsonDecode['data'];

            $cantidad = '0';
            $descuento = '0';
            $precio = '0';
            $ordenDetails = $data[0];
            if(isset($data[4]) && $data[4]!=''){
        	    $descuento = $data[4];
        	}
        	if(isset($data[5]) && $data[5]!=''){
                $precio = $data[5];
        	}
        	if(isset($data[1]) && $data[1]!=''){
        	    $cantidad = $data[1];
            }

        	$updateData[0] = $cantidad;
        	$updateData[1] = $data[2];
        	$updateData[2] = $data[3];
        	$updateData[3] = $precio;
        	$updateData[4] = $descuento;

        	echo actualizarOrdenDetail($updateData, $ordenDetails,$idOrden);

        }
    }
?>

